//index.js
const EventEmitter = require('../../common/EventEmitter');
const User = require('../../common/User');
const config = require('../../config/config');
const requester = require('../../common/Requester');
const util = require('../../common/util');

Page({
    data: {
        deviceInfo: {
            windowHeight: ''
        },
        userInfo: {},
        imageList: '',
        isRefreshing: false,
        hideHomeBack: false,
        isAuthed: false
    },

    onLoad: function (param) {
        let me = this;
        // Get system info and store to appGlobalData.
        util.getSystemInfo();

        // 初始化用户登录数据等,初始化完毕则请求数据，否则弹出失败页面。
        User.checkSession(() => {
            User.getThirdUserInfo()
                .then(res => {
                    let needGetUserInfo = res.data.data.needGetUserInfo;
                    this.setData({
                        isAuthed: needGetUserInfo === 0
                    })
                });
            me.init(param);
        })
    },

    init: function (param) {
        let id = param.id;
        this.id = id;
        this.queryVideoDetail(id);
        this.getRecommendVideoRes(id);
        this.setData({
            hideHomeBack: param.hideHomeBack==1
        });
        EventEmitter.addListener('backFormComment', () => {
            this.queryVideoDetail(id);
        });
    },

    queryVideoDetail: function (id) {
        return requester.request({
            url: requester.service.queryVideoDetail,
            data: {
                resourceId: id
            }
        }).then(res => {
            this.setData({
                video: res.data,
                commentList: res.data.partCommentList,
                isLiked: +res.data.isLiked,
                likeCount: res.data.likeCount
            })
        });
    },

    getRecommendVideoRes: function (id) {
        return requester.request({
            url: requester.service.recommendVideoRes,
            data: {
                resourceId: id
            }
        }).then(res => {
            let video = res.data.simpleVideoResourceVos;
            this.setData({
                videoSource: video
            });
        });
    },

    authComment: function (res) {
        if (res.detail.iv) {
            User.saveUserInfo(res.detail.userInfo)
                .then(() => {
                    console.log('authed');
                    this.goComment();
                });
        } else {
            wx.showToast({
                title: '请先允许授权',
                icon: 'none',
                duration: 1000
            })
        }
    },
    /**
     * 进入评论列表页面。
     */
    goComment: function () {
        let id = this.id;
        wx.navigateTo({
            url: "../comment/comment?id=comment&resourceId=" + id
        });
    },

    back: function () {
        wx.redirectTo({
            url: '../index/index'
        });
    },

    authLike: function (res) {
        console.log(res);
        if (res.detail.iv) {
            User.saveUserInfo(res.detail.userInfo)
                .then(() => {
                    this.likeTap();
                });
        } else {
            wx.showToast({
                title: '请先允许授权',
                icon: 'none',
                duration: 1000
            })
        }
    },
    /**
     * 点赞
     */
    likeTap: function () {
        let id = this.id;
        let liked = this.data.isLiked;
        let likeCount = this.data.likeCount;
        let me = this;

        let resCount = isNaN(likeCount)
            ? likeCount
            : liked ?
                +likeCount - 1
                : +likeCount + 1;

        me.setData({
            isLiked: !liked,
            likeCount: resCount
        });

        requester.request({
            url: !liked ? requester.service.favorComment : requester.service.cancelImageLike,
            data: {
                resourceId: id,
                commentType: 1,
                content: ''
            }
        })
    },

    onVideoCellTap: function (e) {
        let id = e.currentTarget.dataset.id,
            hideHomeBack = this.data.hideHomeBack;
        wx.redirectTo({
            url: '../detail/index?id=' + id + '&hideHomeBack=' + (hideHomeBack?'1':'0')
        });
    },

    onShareAppMessage: function (res) {
        return {
            title: this.data.video.textInfo,
            path: '/pages/detail/index?id=' + this.id,
            imageUrl:this.data.video.smallPicUrl,
            success: function (res) {
                // 转发成功
                wx.showToast({
                    title: '分享成功',
                    icon: 'success',
                    duration: 1000
                })
            },
            fail: function (res) {
                // 转发失败
                wx.showToast({
                    title: '分享失败',
                    icon: 'none',
                    duration: 1000
                })
            }
        }
    }
});
